package com.ufo.all.controller;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;


@Controller
public class testController {


    @GetMapping({"/", "/index"})
    public String toindex(Model m) {
        m.addAttribute("msg", "hello");
        return "index";
    }

//    @PostMapping("/toLogin")
//    public String toLogin2() {
//        return "views/login";
//    }


    @RequestMapping("/toLogin")
    public String toLogin() {
        return "/views/login";
    }

    @RequestMapping("/uu/level1/{id}")
    public String level1(@PathVariable("id") int id) {
        return "views/level1/" + id;
    }

    @RequestMapping("/uu/level2/{id}")
    public String level2(@PathVariable("id") int id) {
        return "views/level2/" + id;
    }

    @RequestMapping("/uu/level3/{id}")
    public String level3(@PathVariable("id") int id) {
        return "views/level3/" + id;
    }

    @RequestMapping("/loginn")
    public String login(String username, String password,Model m) {
        Subject subject = SecurityUtils.getSubject();
        System.out.println(username);
        System.out.println(password);
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);

        try {
            subject.login(token);
            return "index";
        } catch (UnknownAccountException e) {
            m.addAttribute("msg","账户不存在");
            return "views/login";
        }
        catch (IncorrectCredentialsException e){
            m.addAttribute("msg","密码不存在");
            return "views/login";
        }

    }


    @RequestMapping("/noauth")
    public String noauth() {
        return "/noauth";
    }


    @RequestMapping("/logout")
    public String logout(HttpSession session, Model model) {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        model.addAttribute("msg","已安全退出！");
        return "views/login";
    }

}
